Daily Assignment 21

Ecosystem Science and Sustainability 330

Author

Kendall Landwehr

library(dataRetrieval)
Warning: package 'dataRetrieval' was built under R version 4.4.3
library(tidymodels)
── Attaching packages ────────────────────────────────────── tidymodels 1.2.0 ──
✔ broom        1.0.7     ✔ recipes      1.1.1
✔ dials        1.3.0     ✔ rsample      1.2.1
✔ dplyr        1.1.4     ✔ tibble       3.2.1
✔ ggplot2      3.5.1     ✔ tidyr        1.3.1
✔ infer        1.0.7     ✔ tune         1.2.1
✔ modeldata    1.4.0     ✔ workflows    1.1.4
✔ parsnip      1.2.1     ✔ workflowsets 1.1.0
✔ purrr        1.0.2     ✔ yardstick    1.3.1
Warning: package 'recipes' was built under R version 4.4.3
── Conflicts ───────────────────────────────────────── tidymodels_conflicts() ──
✖ purrr::discard() masks scales::discard()
✖ dplyr::filter()  masks stats::filter()
✖ dplyr::lag()     masks stats::lag()
✖ recipes::step()  masks stats::step()
• Learn how to get started at https://www.tidymodels.org/start/
library(dplyr)
library(tsibble)
Warning: package 'tsibble' was built under R version 4.4.3
Registered S3 method overwritten by 'tsibble':
  method               from 
  as_tibble.grouped_df dplyr

Attaching package: 'tsibble'
The following objects are masked from 'package:base':

    intersect, setdiff, union
poudre_flow <- readNWISdv(siteNumber = "06752260",  
                          parameterCd = "00060",    
                          startDate = "2013-01-01",   
                          endDate = "2023-12-31") %>% 
  renameNWISColumns() %>%                             
  mutate(Date = yearmonth(Date)) %>%                  
  group_by(Date)%>%
  
  #1: Converting to tsibble
  summarise(Flow = mean(Flow, na.rm = TRUE)) %>% 
  as_tsibble(index = Date)
GET:https://waterservices.usgs.gov/nwis/dv/?site=06752260&format=waterml%2C1.1&ParameterCd=00060&StatCd=00003&startDT=2013-01-01&endDT=2023-12-31
#2: Plotting time series data

library(ggplot2)

ggplot(poudre_flow, aes(x = Date, y = Flow)) +
  geom_line(color = "steelblue") +
  labs(title = "Monthly Mean Flow at Site 06752260",
       x = "Date",
       y = "Flow (cfs)") +
  theme_minimal()

# Animate with plotly 

library(plotly)
Warning: package 'plotly' was built under R version 4.4.3

Attaching package: 'plotly'
The following object is masked from 'package:ggplot2':

    last_plot
The following object is masked from 'package:stats':

    filter
The following object is masked from 'package:graphics':

    layout
ggplot_obj <- ggplot(poudre_flow, aes(x = Date, y = Flow)) +
  geom_line(color = "steelblue") +
  labs(title = "Monthly Mean Flow at Site 06752260",
       x = "Date",
       y = "Flow (cfs)") +
  theme_minimal()

ggplotly(ggplot_obj)
#3: Subseries 
library(feasts)
Warning: package 'feasts' was built under R version 4.4.3
Loading required package: fabletools
Warning: package 'fabletools' was built under R version 4.4.3

Attaching package: 'fabletools'
The following object is masked from 'package:yardstick':

    accuracy
The following object is masked from 'package:parsnip':

    null_model
The following objects are masked from 'package:infer':

    generate, hypothesize
library(fabletools)
library(ggplot2)

gg_season(poudre_flow, Flow) +
  labs(title = "Seasonal Plot of Streamflow",
       y = "Flow (cfs)") +
  theme_minimal()

# In this plot, seasons are defined by the calendar months, assuming monthly seasonablity. Subseries are time series broken down by season, in this case, one for each month. The data shows a sharp increase in streamflow from April to July, likely because of increased temperatures resulting in more snowmelt and runoff. Rainfall is also typically higher during these months. In 2014, there is outlying data from August to October with a sharp increase in streamflow. 
#4: Decompose 

poudre_flow_stl <- poudre_flow %>%
  model(stl = STL(Flow ~ trend(window = 13) + season(window = "periodic")))

components(poudre_flow_stl) %>%
  autoplot() +
  labs(title = "STL Decomposition of Monthly Streamflow")

# The data is showing strong seasonal peak each year during the spring and early summer likely because of snow melt and resulting runoff. There are lows in the late summer and winter. This shows a seasonal pattern, repeating every 12 months. This also shows long-term changes in streamflow. The streamflow remains fairly consistent with slight decreases in streamflow over the years. This slight change could be a result of humans consuming water resources faster than they can be replaced, reducing overall streamflow.